package org.budo.support.spring.aop.advisor;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractGenericPointcutAdvisor;

/**
 * @author lmw
 */
public class BudoPointcutAdvisor extends AbstractGenericPointcutAdvisor implements Pointcut, ClassFilter, MethodMatcher {
    private static final long serialVersionUID = 1516906705840520615L;

    private static final String[] AOP_SKIP = { //
            "org.springframework." //
    };

    /**
     * 匹配类
     */
    @Override
    public boolean matches(Class<?> clazz) {
        if (Modifier.isFinal(clazz.getModifiers())) {
            return false;
        }

        if (clazz.getName().contains("$")) {
            return false;
        }

        for (String each : AOP_SKIP) {
            if (clazz.getName().startsWith(each)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 匹配方法
     */
    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        return true;
    }

    /**
     * isRuntime
     */
    @Override
    public boolean isRuntime() {
        return false;
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass, Object... args) {
        throw new RuntimeException();
    }

    @Override
    public Pointcut getPointcut() {
        return this;
    }

    @Override
    public ClassFilter getClassFilter() {
        return this;
    }

    @Override
    public MethodMatcher getMethodMatcher() {
        return this;
    }
}